using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Demo.Net.Model;

namespace Demo.Net.EFCoreEnv.Dbcontexts
{
    public class Parking : DbContext
    {
        public Parking(DbContextOptions<Parking> options) : base(options)
        {

        }

        public DbSet<ParkingSpace> ParkingSpaces { get; set; }
        public DbSet<Owner> Owners { get; set; }
        public DbSet<RentRecord> RentRecords { get; set; }
        public DbSet<Feedback> Feedbacks { get; set; }

        // protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        // {
        //     optionsBuilder.UseSqlServer("Parking");
        // }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 如果需要进行表之间的关联配置，请在此处添加代码
            //唯一属性
            modelBuilder.Entity<Owner>()
               .HasIndex(o => o.IdNumber)
               .IsUnique();

            modelBuilder.Entity<Owner>()
                .HasIndex(o => o.Phone)
                .IsUnique();

            modelBuilder.Entity<ParkingSpace>()
                .HasIndex(o => o.Code)
                .IsUnique();
            //修改类型
            // modelBuilder.Entity<Owner>().Property(opt => opt.Home)
            // .HasColumnType("text");
            //软删除
            // modelBuilder.Entity<ParkingSpace>().HasQueryFilter(c => c.IsDeleted == false);
            // modelBuilder.Entity<Owner>().HasQueryFilter(c => c.IsDeleted == false);
            // modelBuilder.Entity<Feedback>().HasQueryFilter(c => c.IsDeleted == false);
            //外键
            modelBuilder.Entity<Feedback>()
            .HasOne(art => art.Owner)
            .WithMany(art => art.feedBacks)
            .HasForeignKey(art => art.OwnerId);

            modelBuilder.Entity<RentRecord>()
            .HasOne(art => art.Owner)
            .WithMany(art => art.RentRecords)
            .HasForeignKey(art => art.OwnerId);

            modelBuilder.Entity<RentRecord>()
            .HasOne(art => art.ParkingSpace)
            .WithMany(art => art.RentRecords)
            .HasForeignKey(art => art.ParkingSpaceId);
            // 其他实体的索引约束...

            base.OnModelCreating(modelBuilder);

            var userInfo1 = new Owner()
            {
                Id = 1,
                Name = "admin",
                Sex = "男",
                IdNumber = "123432345678976543",
                Phone = "13500350606",
                Password = "aaa123456",
                Home = "福建省闽西职业技术学院",
                Room = 220,
                License = "闽A186"
            };
            var userInfo2 = new Owner()
            {
                Id = 2,
                Name = "小李",
                Sex = "女",
                IdNumber = "123432345678976567",
                Phone = "11111111111",
                Password = "123",
                Home = "福建省闽西职业技术学院",
                Room = 110,
                License = "闽A186"
            };
            var userInfo3 = new Owner()
            {
                Id = 3,
                Name = "李涛",
                Sex = "女",
                IdNumber = "450881200205302035",
                Phone = "15977585758",
                Password = "abc123456",
                Home = "福建省闽西职业技术学院",
                Room = 110,
                License = "闽A8888"
            };
            modelBuilder.Entity<Owner>().HasData(
                    userInfo1, userInfo2, userInfo3
            );
            // var car1 = new CarPortType() { Id=1, CarPortTypes = "室内" };
            // var car2 = new CarPortType() { Id=2, CarPortTypes = "室外" };
            // modelBuilder.Entity<CarPortType>().HasData(
            //     car1, car2
            // );
            var PortInfo1 = new ParkingSpace()
            {
                Id = 1,
                Code = "F888",
                Photo = "123.jpg",
                Type = "室内",
                Status = "已出租",
                // IsDeleted = false,
                RentStartTime = DateTime.Now,
                RentEndTime = DateTime.Now,
            };
            var PortInfo2 = new ParkingSpace()
            {
                Id = 2,
                Code = "A666",
                Photo = "222.jpg",
                Type = "室内",
                Status = "未出租",
                // IsDeleted = false,
                RentStartTime = DateTime.Now,
                RentEndTime = DateTime.Now,
            };
            var PortInfo3 = new ParkingSpace()
            {
                Id = 3,
                Code = "D999",
                Photo = "333.jpg",
                Type = "室外",
                Status = "已损坏",
                // IsDeleted = false,
                RentStartTime = DateTime.Now,
                RentEndTime = DateTime.Now,
            };

            modelBuilder.Entity<ParkingSpace>().HasData(
                    PortInfo1, PortInfo2, PortInfo3
            );
            var feedback1 = new Feedback()
            {
                Id = 1,
                OwnerId = 1,
                Title = "车位空间太小",
                Description = "车位空间太小，无法容纳我的大型 SUV。",
                Photo = "img1.jpg",
            };

            var feedback2 = new Feedback()
            {
                Id = 2,
                OwnerId = 2,
                Title = "车位卫生有待改进",
                Description = "车位周边卫生需要加强清理。",
                Photo = "img2.jpg",
            };
            modelBuilder.Entity<Feedback>().HasData(
                    feedback1, feedback2
            );

            var rrecord1 = new RentRecord()
            {
                Id = 1,
                OwnerId = 2,
                ParkingSpaceId = 1,
                RentStartTime = DateTime.Now,
                RentEndTime = DateTime.Now
            };
            var rrecord2 = new RentRecord()
            {
                Id = 2,
                OwnerId = 1,
                ParkingSpaceId = 2,
                RentStartTime = DateTime.Now,
                RentEndTime = DateTime.Now
            };
            modelBuilder.Entity<RentRecord>().HasData(
                rrecord1, rrecord2
            );
        }
    }
}